public class GameInputKey
{
	//cGame_Key_Input
	//input functions of game
	//include: key varibles, key functions
	//////////////////////////////////////Key/////////////////////////////

	  static int s_key = 0;         //current status of all keys
	  static int s_keyPressed = 0;  //keys pressed in current frame
	  static int s_keyReleased = 0; //keys released in current frame
	  static int s_keyCurrent = 0;  //status of all keys in current frame
	  static int s_keyOff = 0;      //released keys during current frame
	  static int s_keycode = 0;     //last pressed key
	  static boolean s_bClearKey;
	  
	  /// virtual functions. will be called by system
	static  public void keyPressed(int keyCode)
	  {
	    s_keycode = keyCode;
	    s_keyCurrent = getKeyMask(keyCode);
	  }

	  protected void keyRepeated(int keyCode)
	  {
	    s_keyCurrent |= getKeyMask(keyCode);
	  }

	static  public void keyReleased(int keyCode)
	  {
	    s_keyOff = s_keyCurrent;
	  }


	  /// key update function. should be called at the begining of every frame
	static  public void keyUpdate()
	  {
	    if(s_bClearKey)
	    {
	      s_key = s_keyCurrent;
	        s_bClearKey = false;

	    }
	    s_keyPressed = ~s_key & s_keyCurrent;
	///    if(s_keyPressed!=0)
///System.out.println("Key "+s_keyPressed);
		///! Code update for key press leak event
		///! Because cellphone which have top running speed, the func keyPress() 
		///! and the func keyRelease() will be occured in almost same time.
		  if (s_keyOff != 0)
		    s_keyCurrent = 0;
	    s_keyOff = 0;
		
	    s_keyReleased = s_key & ~s_keyCurrent;
	    s_key = s_keyCurrent;
	  }
	  
	static public boolean keyIsPressed(int virtualkey)
	{
		  return (s_keyPressed & virtualkey)!=0 ;
		
	}
	
	static public boolean anykeyIsPressed()
	{
		  return s_keyPressed !=0 ;
		
	}
	
	static public void clearAllKeys()
	{
		s_keyPressed = s_keyReleased= s_keyCurrent = s_keyOff = s_key=0;
		
	}
	/// mapping phone's key code to function code
	  /// @param keyCode Device key code
	  /// @return Function key code
	 static  private int getKeyMask(int keyCode)
	  {
		 if(keyCode < 0)
			 keyCode = -keyCode; 
	    switch (keyCode)
	    {
	      case DEF.DEVICE_KEYCODE_LEFTSOFT:
	    	  return DEF.OK;
	      case DEF.DEVICE_KEYCODE_RIGHTSOFT:
	    	  return DEF.CANCEL;
	      case DEF.DEVICE_KEYCODE_UP:
	        return DEF.UP;
	      case DEF.DEVICE_KEYCODE_DOWN:
	        return DEF.DOWN;
	      case DEF.DEVICE_KEYCODE_LEFT:
	     // case 3://patch for sonyErcik750	  
	        return DEF.LEFT;
	      case DEF.DEVICE_KEYCODE_RIGHT:
	     // case 4 ://patch for sonyErcik750
	    	  return DEF.RIGHT;
	      case DEF.DEVICE_KEYCODE_MIDSOFT:
	        return DEF.FIRE;
	      case DEF.DEVICE_KEYCODE_NUM1:
	        return DEF.NUM1;
	      case DEF.DEVICE_KEYCODE_NUM2:
	        return DEF.NUM2;
	      case DEF.DEVICE_KEYCODE_NUM3:
	        return DEF.NUM3;
	      case DEF.DEVICE_KEYCODE_NUM4:
	        return DEF.NUM4;
	      case DEF.DEVICE_KEYCODE_NUM5:
	        return DEF.NUM5;
	      case DEF.DEVICE_KEYCODE_NUM6:
	        return DEF.NUM6;
	      case DEF.DEVICE_KEYCODE_NUM7:
	        return DEF.NUM7;
	      case DEF.DEVICE_KEYCODE_NUM8:
	        return DEF.NUM8;
	      case DEF.DEVICE_KEYCODE_NUM9:
	        return DEF.NUM9;
	      case DEF.DEVICE_KEYCODE_NUM0:
	        return DEF.NUM0;
	      case DEF.DEVICE_KEYCODE_STAR:
	        return DEF.STAR;
	      case DEF.DEVICE_KEYCODE_POUND:
	        return DEF.POUND;

	      default:
	        return 0;
	    }
	  }
}